<template>
  <el-container class="home-container">
    <!-- 头部  每个组件都可以直接是个类名 直接拿来用-->
    <el-header>
      <div>
        <span>电商后台管理平台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区域  -->
        <el-menu
          background-color="#323743"
          text-color="#fff"
          active-text-color="#3386e0"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单的模板区域 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主区 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  // 页面刚一加载的时候，就立即获取左侧菜单  在行为去  定义一个声明周期函数created（）
  // 获取的数据要立即挂载到自己的data
  data() {
    return {
      // 左侧的菜单
      menuList: [],
      iconList: {
        125: 'el-icon-s-custom',
        103: 'el-icon-coin',
        101: 'el-icon-suitcase',
        102: 'el-icon-notebook-1',
        145: 'el-icon-s-marketing',
      },
      // 是否折叠菜单栏
      isCollapse: false,
      // 保存链接的激活状态
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear('token')
      this.$router.push('./login')
    },
    // 获取所有的菜单 {data:  res}  解构赋值  res是属性名 data 是获取的数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menuList = res.data
      console.log(res)
    },
    // 点击按钮对菜单的展开和折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  position: fixed;
  height: 100%;
  width: 100%;
}
.el-header {
  background-color: #000000;
  display: flex;
  justify-content: space-between; //按钮向右对齐
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-menu {
  border-right: none;
}
.el-aside {
  background-color: #323743;
}
.el-button {
  display: flex;
  justify-content: flex-end;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #485065;
  font-size: 10px;
  text-align: center;
  color: #fff;
  line-height: 24px;
  cursor: pointer;
  letter-spacing: 0.2em;
}
</style>
